#include "main.h"
#include "ADC.h"


#define AD_CR_SEL0          (0x00000001)
#define AD_CR_SEL1          (0x00000002)
#define AD_CR_SEL2          (0x00000004)
#define AD_CR_SEL3          (0x00000008)
#define AD_CR_SEL4          (0x00000010)
#define AD_CR_SEL5          (0x00000020)
#define AD_CR_SEL6          (0x00000040)
#define AD_CR_SEL7          (0x00000080)
#define AD_CR_SELMASK       (0x000000ff)
#define AD_CR_CLKDIV        (0x0000ff00)
#define AD_CR_CLKDIVMASK    (0x0000ff00)
#define AD_CR_CLKDIVSHIFT   (8)
#define AD_CR_BURST         (0x00010000)
#define AD_CR_CLKS10        (0x00000000)
#define AD_CR_CLKS9         (0x00020000)
#define AD_CR_CLKS8         (0x00040000)
#define AD_CR_CLKS7         (0x00060000)
#define AD_CR_CLKS6         (0x00080000)
#define AD_CR_CLKS5         (0x000a0000)
#define AD_CR_CLKS4         (0x000c0000)
#define AD_CR_CLKS3         (0x000e0000)
#define AD_CR_PDN           (0x00200000)
#define AD_CR_START_NONE    (0x00000000)
#define AD_CR_START_NOW     (0x01000000)
#define AD_CR_START_P016    (0x02000000)
#define AD_CR_START_P022    (0x03000000)
#define AD_CR_START_MAT01   (0x04000000)
#define AD_CR_START_MAT03   (0x05000000)
#define AD_CR_START_MAT10   (0x06000000)
#define AD_CR_START_MAT11   (0x07000000)
#define AD_CR_START_MASK    (0x07000000)
#define AD_CR_EDGE          (0x08000000)
#define AD_CR_MASK          (0x0f2fffff)


void initADC(void)
{
    PINSEL1 |= (1<<26);  // AD0.2
    PINSEL1 |= (1<<12); // AD1.7
    PCONP |= (1<<20);   //adc1 power
    AD0CR = (1<<21)|(20<<8)|(1<<2);
    AD1CR = (1<<21)|(20<<8)|(1<<7);  //select ad1.7 for bat-cap




}

inline int getADC0(void)
{
    int _ch3,_ch2,_ch1,max;

    AD0CR |= (1<<2);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR2 & (1<<31)));    // wait for the ADC to finish
    _ch1 = (AD0DR2>>6)&0x3FF;
    AD0CR &= ~(1<<2);   // turn off ADC channel
/*    
    
    AD0CR |= (1<<2);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR2 & (1<<31)));    // wait for the ADC to finish
    _ch2 = (AD0DR2>>6)&0x3FF;
    AD0CR &= ~(1<<2);   // turn off ADC channel
  
    AD0CR |= (1<<3);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR3 & (1<<31)));    // wait for the ADC to finish
    _ch3 = (AD0DR3>>6)&0x3FF;
    AD0CR &= ~(1<<3);   // turn off ADC channel
 ///* 
    max = (_ch1>_ch2) ? _ch1:_ch2;
    max = (max<_ch3) ? _ch3:max;

    return max;
   */
    
    return _ch1;
}

inline int getADC1(void)
{
    int _ch3,_ch2,_ch1,max;

//      AD0_CR &= ~(AD_CR_START_MASK | AD_CR_SELMASK);
//  AD0_CR |=  (AD_CR_START_NOW | AD_CR_SEL3);
//  AD0_CR |=   AD_CR_START_NOW;

    AD1CR &=  ~(AD_CR_START_MASK | AD_CR_SELMASK);
    AD1CR |= (AD_CR_START_NOW | AD_CR_SEL7);  // initiate ADC start
    AD1CR |= AD_CR_START_NOW;
    while (!(AD1DR7 & (1<<31)))
    ;    // wait for the ADC to finish
    _ch1 = (AD1DR7>>6)&0x3FF;
//    AD1CR &= ~(1<<7);   // turn off ADC channel
/*    
    
    AD0CR |= (1<<2);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR2 & (1<<31)));    // wait for the ADC to finish
    _ch2 = (AD0DR2>>6)&0x3FF;
    AD0CR &= ~(1<<2);   // turn off ADC channel
  
    AD0CR |= (1<<3);
    AD0CR |= (1<<24);   // initiate ADC start
    while (!(AD0DR3 & (1<<31)));    // wait for the ADC to finish
    _ch3 = (AD0DR3>>6)&0x3FF;
    AD0CR &= ~(1<<3);   // turn off ADC channel
 ///* 
    max = (_ch1>_ch2) ? _ch1:_ch2;
    max = (max<_ch3) ? _ch3:max;

    return max;
   */
    
    return _ch1;
}